<?php
if (!$_GET['id']) {
    echo "Bad Request";
} else {
    $id = $_GET['id'];
//including header.php 
    include '../../include/header.php';
//including page.php (It is a class which contain functions)
    include '../../libraries/page.php';
//created object for page.php
    $page = new page();
    session_start();
    $userid = $_SESSION['userid'];

//To get current date and time
    date_default_timezone_set('Asia/Calcutta');
    $date1 = date("d-m-Y H:i:s");

// checking the session
    if (!$_SESSION['username']) {
        header("location:../user/login.php");
    }
    if ($_GET['q'] == 'logout') {
        $page->user_logout();
        header("location:../user/login.php");
    }
    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $result = $page->mysql_update_array("invoicedetails", $id, "rowid", $_POST);
        if ($result) {
            header("location:stock_register?status=1");
        } else {
            $msg = $message[0];
        }
    }
    ?>

    <!--------------------------------------------------content----------------------------------->

    <!-------------------------------------Navbar---------------------------------->
    <?
    $stock_register = 'active';
    include '../../include/navbar.php';
    ?>

   <div class="bread_crumb" style="margin-left: 2%">
       &laquo;  <a href="stock_register">Back</a>
  </div>

    <!----------------------------------Display the result message of Addition--------------------->
    <div  align="center"><? echo $msg; ?></div>

    <?
    //php code to get the data from invoice table 
    $stock = mysql_query("select * from invoicedetails where rowid = $id");
    $stock_data = mysql_fetch_array($stock);
    $stock_id=$stock_data['productid'];
    ?>

    <!---------------------------------form------------------------------------------------>
    <form method="POST"  action="">
        <div class="maindiv">
            <h3 style="color:#777;text-align: center">Edit Stock</h3>
            <div class="leftdiv">
                <label>Invoice ID</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="invoiceid" value="<?= $stock_data['invoiceid']; ?>" >
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>product</label>
                <select name="productid" style="width:280px" required>
                    <?
                    $products = mysql_query("select * from products where productid=$stock_id");
                    while ($products_data = mysql_fetch_array($products)) {

                    ?>

                    <option value="<?= $products_data['productid']; ?>"><?= $products_data['name']; ?></option>
                    <?
                    }
                    //php code to get the data from products table 
                    $product = mysql_query("select * from products where productid <> $stock_id");
                    while ($product_data = mysql_fetch_array($product)) {
                    ?>
                    <option value="<?= $product_data['productid']; ?>"><?= $product_data['name']; ?></option>
                    <?
                    }
                    ?>
                </select>
            </div>
            <div class="leftdiv">
                <label>Batch No</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="batchno" value="<?= $stock_data['batchno']; ?>" >
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Expiry Date</label>
                <input class="input-xlarge" id="focusedInput" type="datetime-local"  name ="expirydate"  value="<?= $stock_data['expirydate']; ?>">
            </div>
            <div class="leftdiv">
                <label>Qty</label>
                <input class="input-xlarge" id="focusedInput" type="number" name ="qty" value="<?= $stock_data['qty']; ?>"   >
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Free</label>
                <input class="input-xlarge" id="focusedInput" type="number" name ="free" value="<?= $stock_data['free']; ?>"  >
            </div>
            <div class="leftdiv">
                <label>Rate</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="rate" value="<?= $stock_data['rate']; ?>"  >
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>MRP</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="mrp" value="<?= $stock_data['mrp']; ?>"  >

            </div>
            <div class="leftdiv">
                <label>Tax</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="tax" value="<?= $stock_data['tax']; ?>" >
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Total</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="total" value="<?= $stock_data['total']; ?>"  >

            </div>
            <div class="leftdiv">
                <label>Sold</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="sold"  value="<?= $stock_data['sold']; ?>"">
            </div>
            <div class="middiv"></div>
            <div class="rightdiv">
                <label>Bcode</label>
                <input class="input-xlarge" id="focusedInput" type="text" name ="bcode" value="<?= $stock_data['bcode']; ?>"  >

            </div>
            <div class="fulldiv">
                <div class="btn1"><button class="btn btn-inverse"  type="submit" >Update</button></div>
            </div>
        </div>

    </form>
    </div>

    <?php
//including footer.php
    include '../../include/footer.php';
    ?>
    <?php
}
?>